﻿
Imports xmlctrl
Imports xhtmlctrl
Imports webctrl

Public Class posterPreview
    Inherits controlProcessor

    Public Const _pathidentity As String = "/posterPreview"
    Public Const defaultQueryName As String = "t"
    Public Const defaultPvClass As String = Nothing
    Public Const defaultCssFile As String = Nothing
    Private queryName As String = Nothing
    Private pvClass As String = Nothing
    Private cssfile As String = Nothing
    Private inlineCss As String = Nothing
    Private first As Boolean = True

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Private Sub readcss()
        If Not isemptystring(cssfile) Then
            inlineCss = cssrequest.readcss(cssfile)
        End If
    End Sub

    Public Sub New(Optional ByVal queryName As String = defaultQueryName, _
                   Optional ByVal pvClass As String = defaultPvClass, _
                   Optional ByVal cssfile As String = defaultCssFile)
        MyBase.new()
        copy(Me.queryName, queryName)
        copy(Me.pvClass, pvClass)
        copy(Me.cssfile, cssfile)
    End Sub

    Protected Overrides Function processImpl(ByVal context As httpContext, ByRef control As xmlcontrol) As Boolean
        If isdebugmode() OrElse first Then
            readcss()
            first = False
        End If
        Dim page As xhtmlpage = Nothing
        page = context.createControl(New xhtmlpage())
        Dim strs As strings = Nothing
        strs = sp(context)
        page.title().text = strs(stringId.POSTERPREVIEW_TITLE)
        If Not isemptystring(inlineCss) Then
            page.inlineStyle().addstyle("inlineCss", inlineCss, False)
        End If
        Dim c As xmlcontrol = Nothing
        Dim s As String = Nothing
        s = context.url().query(queryName)
        If Not isemptystring(s) Then
            Dim pv As posterViewer = Nothing
            pv = context.createControl(New posterViewer())
            Dim ds As posterViewer.datasource = Nothing
            ds = New posterViewer.datasource()
            ds.page = page
            ds.text = s
            ds.isIEUserAgent = context.isIEUserAgent()
            pv.setData(ds)
            If Not isemptystring(pvClass) Then
                pv.class = pvClass
            End If
            Dim div As div = Nothing
            div = context.createControl(New div())
            div.text = s
            pv.addchild(div)
            c = pv
        Else
            c = context.createControl(New xmltext())
            c.text = strs(stringId.POSTERPREVIEW_NO_INPUT)
        End If

        assert(Not c Is Nothing)
        page.body().addchild(c)
        control = page
        Return True
    End Function
End Class
